/****************************************************************************

Force Engine v0.5

Creado: 16/04/09
Clase: Utilities.h
Hecho by: German Battiston AKA Melkor

****************************************************************************/

//---------------------------------------------------------------------------
#ifndef UTILITIES_H
#define UTILITIES_H
//---------------------------------------------------------------------------
//FUNCIONES UTILES
//---------------------------------------------------------------------------
inline float fsqrt(float fResult) 
{
	float x, y;
	float tempf;
	unsigned long *tfptr = ((unsigned long *)&tempf)+1;

	tempf = fResult;
	*tfptr=(0xbfcd4600-*tfptr)>>1;
	x=tempf;
	y=fResult*0.5f; 
	x*=1.5f-x*x*y; 
	x*=1.5f-x*x*y;
	return x*fResult;
}
//---------------------------------------------------------------------------
inline D3DXVECTOR3 XProduct(D3DXVECTOR3 vVector1, D3DXVECTOR3 vVector2)
{
	D3DXVECTOR3 vResult;
	vResult.x = vVector1.y*vVector2.z - vVector1.z*vVector2.y;
	vResult.y = vVector1.z*vVector2.x - vVector1.x*vVector2.z;
	vResult.z = vVector1.x*vVector2.y - vVector1.y*vVector2.x;

	return vResult;
}
//---------------------------------------------------------------------------
inline float DotProduct(D3DXVECTOR3 vVector1, D3DXVECTOR3 vVector2)
{
	float fResult = 0;

	fResult = vVector1.x * vVector2.x + vVector1.y * vVector2.y + vVector1.z * vVector2.z;

	return fResult;
}
//---------------------------------------------------------------------------
inline float PointDistance(D3DXVECTOR3 vDestination, D3DXVECTOR3 vOrigin)
{
	float fResult = 0;
	float fDestinationXSquare = vDestination.x;
	float fDestinationYSquare = vDestination.y;
	float fDestinationZSquare = vDestination.z;
	float fDestinationModule = 0;
	float fOriginXSquare = vOrigin.x;
	float fOriginYSquare = vOrigin.y;
	float fOriginZSquare = vOrigin.z;
	float fOriginModule = 0;

	fOriginXSquare *= vOrigin.x;
	fOriginYSquare *= vOrigin.y;
	fOriginZSquare *= vOrigin.z;

	fDestinationXSquare *= vDestination.x;
	fDestinationYSquare *= vDestination.y;
	fDestinationZSquare *= vDestination.z;

	fDestinationModule = fsqrt(fDestinationXSquare + fDestinationYSquare + fDestinationZSquare);
	fOriginModule = fsqrt(fOriginXSquare + fOriginYSquare + fOriginZSquare);

	fResult = fOriginModule - fDestinationModule;

	return fResult;
}

//---------------------------------------------------------------------------

#endif /*--- UTILITIES_H ---*/

//---------------------------------------------------------------------------